//
//  GPTextMessageCell.m
//  GuaPi
//
//  Created by 陈华 on 2017/6/21.
//  Copyright © 2017年 Joanlove. All rights reserved.
//

#import "GPTextMessageCell.h"
#import "GPEmotionManager.h"

@interface GPTextMessageCell ()

/**文本内容label*/
@property(nonatomic,weak) UILabel *textContentLabel;

@end

@implementation GPTextMessageCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        //Label
        UILabel *textContentLabel = [[UILabel alloc]init];
        textContentLabel.numberOfLines = 0;
        textContentLabel.font = [UIFont systemFontOfSize:14.0];
        textContentLabel.textColor = [UIColor blackColor];
        textContentLabel.textAlignment = NSTextAlignmentLeft;
        textContentLabel.preferredMaxLayoutWidth = kScreenWidth - 2 * (43 + 10 + 10) - 40;
        [self.contentBackground addSubview:textContentLabel];
        self.textContentLabel = textContentLabel;

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapContent)];
        [self.contentView addGestureRecognizer:tap];
        
    }
    return self;
}



- (void)setMessage:(GPMessageItem *)message
{
    //设置父类的值和约束
    [super setMessage:message];
    
    //设置子类的文本相关值
    [self setTextValue:message];
    
    //设置子类的文本相关约束
    [self changeTextConstraint:message];
}

/**设置文本cell的值*/
- (void)setTextValue:(GPMessageItem *)message
{
    self.textContentLabel.attributedText = [GPEmotionManager textWithImageString:message.text font:self.textContentLabel.font];
    if (message.isSender) {
        self.textContentLabel.textColor = [UIColor whiteColor];
        
        
    }else{
        self.textContentLabel.textColor = [UIColor blackColor];
    }
}

/**改变文本cell的约束*/
- (void)changeTextConstraint:(GPMessageItem *)message
{
    //如果是本人
    if (message.isSender) {
        [self.textContentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(self.contentBackground.mas_left).offset(10);
//            make.right.mas_equalTo(self.contentBackground.mas_right).offset(-30);
            make.right.mas_equalTo(self.contentBackground.mas_right).offset(-10);
            make.top.mas_equalTo(self.contentBackground.mas_top).offset(10);
            make.bottom.mas_equalTo(self.contentBackground.mas_bottom).offset(-10);
            
        }];
        
    }
    //如果是对方
    else{
        [self.textContentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            
//            make.left.mas_equalTo(self.contentBackground.mas_left).offset(30);
            make.left.mas_equalTo(self.contentBackground.mas_left).offset(10);
            make.right.mas_equalTo(self.contentBackground.mas_right).offset(-10);
            make.top.mas_equalTo(self.contentBackground.mas_top).offset(10);
            make.bottom.mas_equalTo(self.contentBackground.mas_bottom).offset(-10);
            
        }];
        
    }
    
    //更新
    [self layoutIfNeeded];
    
    //计算cell的高度
    CGFloat cellHeight = CGRectGetMaxY(self.contentBackground.frame) + 10;
    message.cellHeight = cellHeight;
}

- (void)tapContent
{
    if ([self.delegate respondsToSelector:@selector(messageCell:didContentClicked:)]) {
        [self.delegate messageCell:self didContentClicked:self.message];
    }
}


@end
